package com.controller.sys;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.common.easyui.EasyuiUtils;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.common.easyui.JsonTreeData;
import com.dao.sys.SysDeptMapper;
import com.dao.sys.SysLogMapper;
import com.dao.sys.SysManageMapper;
import com.model.sys.SysDept;
import com.service.sys.SysLogService;

@Controller
@RequestMapping(value = "/api")
public class DeptManageController {
    static final Logger logger = LoggerFactory.getLogger(DeptManageController.class);

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysManageMapper sysManageMapper;
    @Autowired
    private SysLogMapper sysLogMapper;
    @Autowired
    private SysLogService sysLogService;

    @RequestMapping(value = "/DeptManage/AddDept", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String AddDept(HttpServletRequest request) {
        try {
            String resultString = "保存成功!";
            SysDept model = new SysDept();
            String deptid = request.getParameter("deptid");
            // model = sysDeptMapper.selectByPrimaryKey(model.getDwbm());
            String userString = request.getParameter("userid");
            model.setDeptid(deptid);
            model.setDydwbm(request.getParameter("dydwbm"));
            if (deptid == null || deptid.equals("")) {
                model = new SysDept();
                // if (dal.ExistDYDWBM(model.getDydwbm())) {
                // resultString = "锟剿伙拷锟斤拷锟斤拷锟斤拷锟窖达拷锟斤拷!";
                // return resultString;
                // }
                String uuid = UUID.randomUUID().toString();
                logger.info("userdept:{}", uuid);
                model.setDeptid(uuid);
            } else {
                // if (dal.ExistDYDWBM(model.getDydwbm(), model.getDwbm())) {
                // resultString = "锟剿伙拷锟斤拷锟斤拷锟斤拷锟窖达拷锟斤拷!";
                // return resultString;
                // }
            }
            model.setDwbm(request.getParameter("dwbm"));
            model.setDwmc(request.getParameter("dwmc"));
            model.setParentid(request.getParameter("pid"));
            model.setXssx(Integer.parseInt(request.getParameter("xssx")));
            model.setBz1("");
            model.setBz3("");
            model.setDisabled("");
            model.setJgsx(request.getParameter("jgsx"));
            if (deptid == null || deptid.equals("")) {
                resultString = sysDeptMapper.insert(model) > 0 ? "保存成功!" : "保存失败!";
//                sysLogService.add(userString, "新增机构_" + deptid);
            } else {
                resultString = sysDeptMapper.updateByPrimaryKey(model) > 0 ? "保存成功!" : "保存失败!";
//                sysLogService.add(userString, "修改机构_" + deptid);
            }
            return resultString;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "" + e.getMessage());
            logger.error("errorMessage:{}", e.getMessage());
            return e.getMessage();
        }
    }

    @RequestMapping(value = "/DeptManage/DelDept", produces = "text/plain;charset=UTF-8")
    @ResponseBody
    public String DelDept(HttpServletRequest request) {
        try {
            String resultString = "保存成功!";
            String dwbm = request.getParameter("deptid");
            resultString = sysDeptMapper.deleteByPrimaryKey(dwbm) > 0 ? "保存成功!" : "保存失败!";
            String userString = request.getParameter("userid");
//            sysLogService.add(userString, "删除机构_" + dwbm);
            return resultString;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "" + e.getMessage());
            logger.error("errorMessage:{}", e.getMessage());
            return e.getMessage();
        }
    }

    @RequestMapping("/DeptManage/DelDept1")
    public Map<String, String> DelDept1() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "value-1");
        map.put("key2", "value-2");
        map.put("key3", "value-2");
        return map;
    }

    @RequestMapping(value = "/DeptManage/DeptListJson", produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String DeptListJson(HttpServletRequest request) {
        try {
            List<SysDept> list = sysDeptMapper.selectAll();
            String deptid = request.getParameter("deptid");
            SysDept modelDept = sysDeptMapper.selectByPrimaryKey(deptid);
            String jsonString = EasyuiUtils.getOwnTreeGridJson(list, "deptid", "parentid", modelDept.getParentid(),
                    modelDept.getDeptid());

            // System.out.println("DeptListJson:" + jsonString);
            logger.info("DeptListJson:{}", jsonString);
            // request.setAttribute("addLists", list);
            return jsonString;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "" + e.getMessage());
            logger.error("errorMessage:{}", e.getMessage());
            return e.getMessage();
        }
    }

    @RequestMapping(value = "/DeptManage/DeptListJson_combobox", produces = "text/json;charset=UTF-8")
    @ResponseBody
    public String DeptListJson_combobox(HttpServletRequest request) {
        try {
            List<SysDept> list = sysDeptMapper.selectAll();
            String deptid = request.getParameter("deptid");
            SysDept modelDept = sysDeptMapper.selectByPrimaryKey(deptid);
//			String jsonString = EasyuiHelper.getCobTreeJson(list, "deptid", "dwmc", "parentid", modelDept.getParentid(),
//					"", false);

            List<JsonTreeData> treeDataList = new ArrayList<JsonTreeData>();
            for (SysDept sysDept : list) {
                JsonTreeData treeData = new JsonTreeData();
                treeData.setId(sysDept.getDeptid());
                treeData.setPid(sysDept.getParentid());
                treeData.setText(sysDept.getDwmc());
                treeDataList.add(treeData);
            }
            List<JsonTreeData> newTreeDataList = EasyuiUtils.getTreeJson(treeDataList, modelDept.getParentid(),
                    modelDept.getDeptid());
            String slist = JSON.toJSONString(newTreeDataList);

            logger.info("DeptListJson_combobox:{}", slist);
            request.setAttribute("addLists", list);
            return slist;
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("errorMessage", "" + e.getMessage());
            logger.error("errorMessage:{}", e.getMessage());
            return e.getMessage();
        }
    }
}
